package com.jt.service;

import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-12 15:19
 */
@Service
public class HttpClientServiceImpl implements HttpClientService {

    @Override
    public List<User> findUserList() {
        List<User> userList = new ArrayList<>();
        String url = "http://sso.jt.com/user/findUserList";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                String json = EntityUtils
                        .toString(httpEntity, "UTF-8");
                userList = ObjectMapperUtil
                        .toObject(json, userList.getClass());
            } else {
                System.out.println("你访问的页面走丢了 - -!");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return userList;
    }

}
